package com.example.controller;


import com.example.dao.TrainingCycleCrudRepository;
import com.example.pojo.PageBean;
import com.example.pojo.TrainingCycle;
import com.example.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/trainingCycle")
@Slf4j
public class TrainingCycleController {
    @Autowired
    private TrainingCycleCrudRepository trainingCycleCrudRepository;


    @PostMapping("/addTrainingCycle")
    public Result add(@RequestBody TrainingCycle trainingCycle) {
        TrainingCycle save = trainingCycleCrudRepository.save(trainingCycle);
        return Result.success(save);
    }

    @DeleteMapping("/deleteTrainingCycle")
    public Result delete(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            trainingCycleCrudRepository.deleteById(id);
        }
        return Result.success("删除成功");
    }

    @PostMapping("/updateTrainingCycle")
    public Result update(@RequestBody TrainingCycle trainingCycle) {
        TrainingCycle save = trainingCycleCrudRepository.save(trainingCycle);
        return Result.success(save);
    }

    @PostMapping("/getAllTrainingCycle")
    public Result getAllTrainingCycle(@RequestBody TrainingCycle trainingCycle,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("trainingCycle:{}", trainingCycle);
        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.Direction.ASC, "id");

        Page<TrainingCycle> coursePage = trainingCycleCrudRepository.findAll(new Specification<TrainingCycle>() {
            @Override
            public Predicate toPredicate(Root<TrainingCycle> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //获取条件对象
                Predicate predicate = cb.conjunction();
                //判断用户对象
                if (trainingCycle != null) {
                    // 根据 student 的属性添加查询条件
                    if (trainingCycle.getCycle() != null) {
                        predicate.getExpressions().add(cb.equal(root.get("cycle"), trainingCycle.getCycle()));
                    }
                    if (trainingCycle.getId() != null) {
                        predicate.getExpressions().add(cb.equal(root.get("id"), trainingCycle.getId()));
                    }
                }
                return predicate;
            }
        }, pageable);
        List<TrainingCycle> TrainingCycles = coursePage.getContent();
        long elements = coursePage.getTotalElements();
        return Result.success(new PageBean(elements, TrainingCycles));
    }

    @GetMapping("/getTrainingCycleList")
    public Result getTrainingCycleList() {
        Iterable<TrainingCycle> all = trainingCycleCrudRepository.findAll();
        List<TrainingCycle> TrainingCycleList = new ArrayList<>();
        for (TrainingCycle trainingCycle : all) {
            TrainingCycleList.add(trainingCycle);
        }
        return Result.success(TrainingCycleList);
    }

}
